Skip to content

Switch to AsyncAnthropic client#12

Merged
mattgodbolt merged 1 commit intomainfrom
molty/async-client
Feb 21, 2026
Merged

Switch to AsyncAnthropic client#12
mattgodbolt merged 1 commit intomainfrom
molty/async-client

Conversation

@mattgodbolt-molty
Copy link
Contributor

Fixes #9

The sync Anthropic client was being used inside async FastAPI routes, blocking the event loop during API calls. As @Alc-Alc correctly identified, this would affect throughput under concurrent requests.

Changes

  • app/explain.py: AnthropicAsyncAnthropic, await client.messages.create()
  • app/main.py: Anthropic()AsyncAnthropic() in lifespan
  • app/test_explain.py: MagicMockAsyncMock for the messages.create mock

All 91 tests pass, live smoke test confirmed working.

(I'm Molty, an AI assistant acting on behalf of @mattgodbolt)

The sync Anthropic client was blocking the event loop inside async
FastAPI routes. Switch to AsyncAnthropic so the API call properly
yields to the event loop while waiting for the response.

Closes #9

🤖 Generated by LLM (Claude, via OpenClaw)
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #9 by switching from the synchronous Anthropic client to the asynchronous AsyncAnthropic client throughout the codebase. The sync client was blocking the event loop in async FastAPI routes, which would degrade throughput under concurrent requests. This change ensures all API calls to Anthropic are properly awaited and non-blocking.

Changes:

  • Replaced Anthropic with AsyncAnthropic client instantiation and imports
  • Added await to client.messages.create() API call
  • Updated test mocks from MagicMock to AsyncMock for async method simulation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
app/main.py Updated client instantiation from Anthropic() to AsyncAnthropic() in the lifespan context manager
app/explain.py Changed import from Anthropic to AsyncAnthropic, updated type hints, and added await to the API call
app/test_explain.py Updated test mocks to use AsyncMock for the async messages.create method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mattgodbolt mattgodbolt merged commit e6d8cee into main Feb 21, 2026
6 checks passed
@mattgodbolt mattgodbolt deleted the molty/async-client branch February 21, 2026 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarifications regarding the Anthropic Client

3 participants